package models

import (
	"github.com/beego/beego/v2/client/orm"
	"strings"
	"time"
)

type DoDiagnosis struct {
	Id        int64     `json:"id"`
	Archives  *Archives `orm:"rel(fk);null;on_delete(do_nothing)" json:"archives"`
	Guahao    *DoGuahao `orm:"rel(fk);null;on_delete(do_nothing)" json:"guahao"`
	Content   string    `json:"content"`
	CreatedAt int64     `json:"created_at"`
}

func (a *DoDiagnosis) TableName() string {
	return DoDiagnosisTableName()
}

func DoDiagnosisTableName() string {
	return TableName("do_diagnosis")
}

//查询诊断列表
func DoDiagnosisList(archives_id, guahao_id int64) []*DoDiagnosis {
	query := orm.NewOrm().QueryTable(DoDiagnosisTableName())
	data := make([]*DoDiagnosis, 0)
	cond := orm.NewCondition()
	cond = cond.And("archives_id", archives_id).And("guahao_id", guahao_id)
	query.SetCond(cond).OrderBy("-id").All(&data)
	return data
}

//查询诊断
func DoDiagnosisQuery(archives_id, guahao_id int64) *DoDiagnosis {
	query := orm.NewOrm().QueryTable(DoDiagnosisTableName())
	data := new(DoDiagnosis)
	cond := orm.NewCondition()
	cond = cond.And("archives_id", archives_id).And("guahao_id", guahao_id)
	query.SetCond(cond).OrderBy("-id").One(data)
	return data
}

//保存诊断
func DoDiagnosisSave(model *DoDiagnosis) (*DoDiagnosis, error) {
	model.Content = strings.Trim(model.Content, " ")
	model.CreatedAt = time.Now().Unix()
	if model.Id > 0 {
		if _, err := orm.NewOrm().Update(model, "Content"); err != nil { //新增
			return nil, err
		}
	} else {
		if _, err := orm.NewOrm().Insert(model); err != nil { //新增
			return nil, err
		}
	}
	return model, nil
}
